package cn.ges.redis;

import cn.ges.type.TypeHelper;
import org.springframework.lang.Nullable;

import java.math.BigDecimal;

/**
 * 为统计数据 便捷操作的 工具类
 *
 * @author gespent@163.com
 * @date 2019/09/17 16:58
 */
public class CountHelper {

    public static final int TWO_DAY_SECONDS = 86400;

    /**
     * 在hash中保存用户 执行该操作的次数 每次执行 次数+1
     */
    public static void recordUserTime(String key, long userId, Integer timeout) {
        final String s = ((Long) userId).toString();
        if (RedisUtil.hasKey(key)) {
            int integer = getUserValue(key, userId);
            RedisUtil.hset(key, s, integer + 1);
        } else {
            if (timeout == null) {
                RedisUtil.hset(key, s, 1, TWO_DAY_SECONDS);
            } else {
                RedisUtil.hset(key, s, 1, timeout);
            }
        }
    }

    /**
     * 在hash中保存用户 执行该操作的次数 每次执行 次数增加指定值
     */
    public static void recordUserTime(String key, long userId, BigDecimal step, Integer timeout) {
        final String s = ((Long) userId).toString();
        if (RedisUtil.hasKey(key)) {
            BigDecimal userBigDecimalValue = getUserBigDecimalValue(key, userId);
            RedisUtil.hset(key, s, userBigDecimalValue.add(step).toString());
        } else {
            if (timeout == null) {
                RedisUtil.hset(key, s, 1, TWO_DAY_SECONDS);
            } else {
                RedisUtil.hset(key, s, 1, timeout);
            }
        }
    }

    /**
     * 在hash中保存用户 执行该操作的次数 每次执行 次数增加指定值
     */
    public static void recordUserTime(String key, long userId, Integer step, Integer timeout) {
        final String s = ((Long) userId).toString();
        if (RedisUtil.hasKey(key)) {
            int userValue = getUserValue(key, userId);
            RedisUtil.hset(key, s, userValue + step);
        } else {
            if (timeout == null) {
                RedisUtil.hset(key, s, 1, TWO_DAY_SECONDS);
            } else {
                RedisUtil.hset(key, s, 1, timeout);
            }
        }
    }

    /**
     * 在hash中保存用户 某个值
     */
    public static void recordUserValue(String key, long userId, Object value, Integer timeout) {
        final String s = ((Long) userId).toString();
        if (RedisUtil.hasKey(key)) {
            RedisUtil.hset(key, s, value);
        } else {
            if (timeout == null) {
                RedisUtil.hset(key, s, value, TWO_DAY_SECONDS);
            } else {
                RedisUtil.hset(key, s, value, timeout);
            }
        }
    }

    /**
     * 获得用户的值（hash）
     * 返回值不会有null
     */
    public static int getUserValue(String key, Long userId) {
        final Object hget = RedisUtil.hget(key, userId.toString());
        final Integer integer = TypeHelper.number2Int(hget);
        if (integer == null) {
            return 0;
        }
        return integer;
    }

    public static BigDecimal getUserBigDecimalValue(String key, Long userId) {
        final Object hget = RedisUtil.hget(key, userId.toString());
        final BigDecimal bigDecimal = TypeHelper.number2BIgDecimal(hget);
        if (bigDecimal == null) {
            return BigDecimal.ZERO;
        }
        return bigDecimal;
    }

    /**
     * 获得用户的值（hash）
     */
    public static Integer getUserValueWithNull(String key, Long userId) {
        final Object hget = RedisUtil.hget(key, userId.toString());
        return TypeHelper.number2Int(hget);
    }


    public static void addNumber2Redis(String key, int step, @Nullable Integer timeout) {
        RedisUtil.sNumberMerge(key, step, step, timeout);
    }

    public static void addChannelNumber2Redis(String key, String channel, int step, @Nullable Integer timeout) {
        RedisUtil.sNumberMerge(key + channel, step, step, timeout);
    }

    public static void addChannelAndMainNumber2Redis(String key, String channel, int step, @Nullable Integer timeout) {
        RedisUtil.sNumberMerge(key, step, step, timeout);
        RedisUtil.sNumberMerge(key + channel, step, step, timeout);
    }

    public static int getNumberFromRedis(String key) {
        final Object o = RedisUtil.get(key);
        Integer integer = TypeHelper.number2Int(o);
        if (integer == null) {
            integer = 0;
        }
        return integer;
    }

    public static void addPeopleNumber2Redis(String key, long userId, long time) {
        RedisUtil.sSetAndTime(key, time, userId);
    }

    public static void addChannelPeopleNumber2Redis(String key, String channel, long userId, long time) {
        RedisUtil.sSetAndTime(key + channel, time, userId);
    }

    public static void addChannelAndMainPeopleNumber2Redis(String key, String channel, long userId, long time) {
        RedisUtil.sSetAndTime(key, time, userId);
        RedisUtil.sSetAndTime(key + channel, time, userId);
    }

    public static int getPeopleNumberFromRedis(String key) {
        return (int) RedisUtil.sGetSetSize(key);
    }

    public static String getChannelKey(String key, String channel) {
        return key + channel;
    }


    public static void addDecimal2Redis(String key, BigDecimal step, @Nullable Integer timeout) {
        RedisUtil.sBigDecimalMerge(key, step, step, timeout);
    }

    public static void addChannelDecimal2Redis(String key, String channel, BigDecimal step, @Nullable Integer timeout) {
        RedisUtil.sBigDecimalMerge(key + channel, step, step, timeout);
    }

    public static void addChannelAndMainDecimal2Redis(String key, String channel, BigDecimal step, @Nullable Integer timeout) {
        RedisUtil.sBigDecimalMerge(key, step, step, timeout);
        RedisUtil.sBigDecimalMerge(key + channel, step, step, timeout);
    }

    public static BigDecimal getDecimalFromRedis(String key) {
        final Object o = RedisUtil.get(key);
        return TypeHelper.number2BIgDecimal(o);
    }


}
